package com.dreamsail.core.controller;

import com.dreamsail.core.dto.MenuDto;
import com.dreamsail.core.entity.UserInfo;
import com.dreamsail.core.frame.SkipLogin;
import com.dreamsail.core.model.LoginReq;
import com.dreamsail.core.model.system.PageInfo;
import com.dreamsail.core.model.system.PageReq;
import com.dreamsail.core.model.system.Result;
import com.dreamsail.core.service.MenuService;
import com.dreamsail.core.service.UserService;
import com.dreamsail.core.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户控制
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;

    /**
     * 登录接口
     * @param loginReq
     * @return
     */
    @SkipLogin
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginReq loginReq){
        UserInfo userInfo = userService.login(loginReq.getAccount(),loginReq.getPassword());
        String token = JwtUtil.acquireJWT(userInfo.getUserName(), userInfo.getId());
        return new Result<>(token);
    }


    @GetMapping("info")
    public Result<UserInfo> getInfo(){
        return new Result<>(userService.getInfo());
    }


    /**
     * 注册接口(新增用户)
     * @param loginReq
     * @return
     */
    @PostMapping("sign")
    public Result<Boolean> sign(@RequestBody LoginReq loginReq){
        userService.sign(loginReq.getAccount(),loginReq.getPassword());
        return new Result<>(true);
    }

    /**
     * 用户更新密码
     * @param password 密码
     * @return
     */
    @GetMapping("update/password")
    public Result<Boolean> updatePassword(String password){
        userService.updatePassword(password);
        return new Result<>(true);
    }

    /**
     * 获取当前用户菜单
     * @return
     */
    @GetMapping("menu")
    public Result<List<MenuDto>> getMenu(){
        List<MenuDto> menuDtos = menuService.getMenu();
        return new Result<>(menuDtos);
    }

    /**
     * 用户分页请求
     * @param pageReq
     * @return
     */
    @PostMapping("query/page")
    public Result<PageInfo<UserInfo>> queryUserPage(@RequestBody PageReq<UserInfo> pageReq){
        return new Result<>(userService.queryUserPage(pageReq));
    }

}
